using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Voice;
using System.Collections.Generic;
using Ent.Web.Helper;

public partial class Admin_ManageUser : System.Web.UI.Page
{
    int count = 5;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateList();
        }
    }
    private void PopulateList()
    {
        try
        {
            int pi = MathFunction.ConvertToInteger(Request.QueryString["pi"], 0);
            pi = pi == 0 ? 1 : pi;
            int start = (pi - 1) * count + 1;

            int total = 1;
            DataTable dt = GetUserList(ref total, start);
            int pageCount = dt.Rows.Count;
            lblHeader.Text = ResourceHelper.Get("MSG_SearchHeader") + " " + start.ToString() + " to " + Convert.ToInt32(start + pageCount - 1) + " of " + total.ToString() + " records.";
            SetNavigation(pi, total, start);
            if (dt != null && dt.Rows.Count != 0)
            {
                gvUser.DataSource = dt;
                gvUser.DataBind();
            }
            else
            {
                lblHeader.Text = ResourceHelper.Get("MSG_NoResult");
                gvUser.Visible = false;
            }
        }
        catch (Exception exc)
        {
            Common.ErrorPage(exc);
        }
    }
    private DataTable GetUserList(ref int total, int startIndex)
    {
        DataTable retValue = new DataTable();
        retValue.Columns.Add("Active");
        retValue.Columns.Add("Id");
        retValue.Columns.Add("Name");
        retValue.Columns.Add("Location");
        retValue.Columns.Add("Hobbies");

        Ent.Voice.User[] us = Ent.Voice.User.ListAllUsers(startIndex, count, "Active", ref total);
        if (us == null)
            return retValue;
        for (int i = 0; i < us.Length; i++)
        {
            User u = (User)us.GetValue(i);
            DataRow dr = retValue.NewRow();
            dr["Active"] = u.Active;
            dr["Id"] = u.UserID;
            dr["Name"] = u.FName + " " + u.LName;
            dr["Location"] = u.Location;
            dr["Hobbies"] = u.Hobbies;

            if (dr != null)
                retValue.Rows.Add(dr);
        }
        return retValue;
    }
    private void SetNavigation(int pi, int total, int start)
    {
        Dictionary<string, string> kvList = new Dictionary<string, string>();
        kvList.Add("pi", "");
        btnPrev.Visible = false;
        btnNext.Visible = false;
        if (pi > 1)
        {
            btnPrev.Visible = true;
            kvList["pi"] = Convert.ToString(pi - 1);
            btnPrev.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
        if (total >= start + count)
        {
            btnNext.Visible = true;
            kvList["pi"] = Convert.ToString(pi + 1);
            btnNext.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
    }
    protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Activate")
        {
            string userId = e.CommandArgument.ToString();
            Ent.Voice.User.ToggleActivation(userId);
            PopulateList();
            // Activate UserId
        }
    }
    protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[1].Visible = false;
        e.Row.Cells[2].Visible = false;
    }
    public string GetImage(string isActive)
    {
        return (isActive.ToLower() == "true" 
            ? ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/success.gif"
            : ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/err.gif");
    }
    public string GetActivationText(string isActive)
    {
        return (isActive.ToLower() == "true" ? "Deactivate" : "Activate");
    }
}
